import { ref } from 'vue'
import { defineStore } from 'pinia'

// 用户信息类型定义
export interface UserInfo {
  id?: number
  username?: string
  role?: string
  email?: string
  phone?: string
  avatar?: string
  nickname?: string
  bio?: string
  created_at?: string
  updated_at?: string
}

export const useAuthStore = defineStore(
  'auth',
  () => {
    const token = ref('')
    const userInfo = ref<UserInfo | null>(null) // 使用UserInfo类型

    const login = async () => {
      token.value = '111'
      // 模拟用户信息
      userInfo.value = {
        username: '测试用户',
        role: 'student'
      }
      return { success: true }
    }

    const refreshUserInfo = async () => {
      // 实际项目中应该从API获取用户信息
    }

    function clearToken() {
      token.value = ''
      userInfo.value = null
      localStorage.removeItem('token')
      localStorage.removeItem('auth')
    }

    return { token, clearToken, login, userInfo, refreshUserInfo }
  },
  {
    persist: true,
  },
)
